package com.kfh.ybracelet.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.utils.Constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 充值选择金额的adapter
 * Created by Aaron on 16/9/8.
 */
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ChooseMoneyViewHolder> {

    private static JSONArray orderList;
    private static Context ctx;
    private LayoutInflater mLayoutInflater;

    private final String TAG = this.getClass().getSimpleName();

    public OrderListAdapter(Context ctx, JSONArray orderList) {
        this.ctx = ctx;
        this.mLayoutInflater = LayoutInflater.from(ctx);
        this.orderList = orderList;
    }

    @Override
    public int getItemCount() {
        return orderList.length();
    }

    @Override
    public ChooseMoneyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChooseMoneyViewHolder(mLayoutInflater.inflate(R.layout.item_order, parent, false));
    }

    @Override
    public void onBindViewHolder(ChooseMoneyViewHolder holder, final int position) {

        try {
            if (orderList == null || orderList.get(position) == null)
                return;

            JSONObject o = (JSONObject) orderList.get(position);

//            String body = o.getString("body");
//            JSONObject b = new JSONObject(body);

//            if (TextUtils.isEmpty(b.getString("address"))) {
//
//                LogUtil.e(TAG, "这是充值 订单" + position);
////                holder.btn_detail.setText("详情");
//            } else {
//
//                LogUtil.e(TAG, "这是新开卡 订单" + position);
////                holder.btn_detail.setText("查看物流");
//            }

            String pay_style = "";
            switch (o.getString("payType")) {
                case Constant.PAY_STYLE_ALIPAY:
                    pay_style = "支付宝";
                    break;
                case Constant.PAY_STYLE_WEIXIN:
                    pay_style = "微信";
                    break;
                default:
                    pay_style = "其他";
                    break;
            }

//            String orderTime = "";
//
//            try {
////                String payTime = o.getString("payTime").replace("Z", "UTC");
////                payTime = payTime.replace("Z", "UTC");
////                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
////                Date dt = sdf.parse(o.getString("payTime").replace("Z", "UTC"));
////                TimeZone tz = sdf.getTimeZone();
////                Calendar c = sdf.getCalendar();
////                LogUtil.e(TAG, "订单时间: "  + getString(c));
////                LogUtil.e(TAG, "订单时间: " + CommonUtils.getLocalTime(o.getString("payTime")));
//
//                orderTime = CommonUtils.getLocalTime(o.getString("payTime"));
//                orderTime = o.getString("payTime");
//
//            } catch (ParseException e) {
//                e.printStackTrace();
//                LogUtil.e(TAG, "addTime 时间解析失败");
//            }

            //价格
            holder.tv_price.setText("价格: " + o.getString("fee") + "元");
//            holder.tv_price.setText("价格: " + o.getString("fee") + "元" + "    " + (o.getString("tradeState").equals("SUCCESS") ? "支付成功" : "支付失败"));
            //订单号
            holder.tv_num.setText("订单号: " + o.getString("tradeNo"));
            //商品名称
            holder.tv_goods.setText(o.getString("subject"));
            //下单时间
            holder.tv_time.setText("支付时间: " + o.getString("addTime"));
            //支付方式
            holder.tv_style.setText("支付方式: " + pay_style);
            //支付状态图标
            if (o.getString("tradeState").equals("SUCCESS")) {
                //支付成功
                holder.iv_status.setImageResource(R.mipmap.ic_order_success);
                holder.tv_detail.setVisibility(View.VISIBLE);
                holder.view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (itemClickListener != null) {
                            itemClickListener.onclick(position);
                        }

                    }
                });
            } else {
                //支付失败
                holder.iv_status.setImageResource(R.mipmap.ic_order_fail);
                holder.tv_detail.setVisibility(View.INVISIBLE);
                holder.view.setOnClickListener(null);
            }

//            holder.iv_status.setImageResource((o.getString("tradeState").equals("SUCCESS") ? R.mipmap.ic_order_success : R.mipmap.ic_order_fail));

//            String text = "订单号: " + o.getString("tradeNo") +
//                    "\n\n套餐:" + o.getString("subject") +
//                    "\n\n支付时间: " + orderTime +
//                    "\n\n支付方式: " + pay_style +
////                            "\n支付状态:" + (o.getString("tradeState").equals("SUCCESS") ?
////                            Html.fromHtml("<font color='#fc213e'>" + "支付成功" + "</font>") :
////                            Html.fromHtml("<font color='#fc213e'>" + "支付失败" + "</font>")));
//                    "\n\n支付状态: " + (o.getString("tradeState").equals("SUCCESS") ? "支付成功" : "支付失败");
//
//            SpannableStringBuilder builder = new SpannableStringBuilder(text);
//            ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
//
//            TextAppearanceSpan span = new TextAppearanceSpan(null, 0, 45, null, null);
//
//            builder.setSpan(redSpan, text.length() - 4, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//            builder.setSpan(span, text.length() - 4, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//            holder.tv_order_info.setText(builder);


        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

//    private String getString(Calendar c) {
//        StringBuffer result = new StringBuffer();
//        result.append(c.get(Calendar.YEAR));
//        result.append("-");
//        result.append((c.get(Calendar.MONTH) + 1));
//        result.append("-");
//        result.append(c.get(Calendar.DAY_OF_MONTH));
//        result.append("  ");
//        result.append(c.get(Calendar.HOUR_OF_DAY));
//        result.append(":");
//        result.append(c.get(Calendar.MINUTE));
//        result.append(":");
//        result.append(c.get(Calendar.SECOND));
//        return result.toString();
//    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ChooseMoneyViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_price;
        private TextView tv_num;
        private TextView tv_goods;
        private TextView tv_time;
        //        private Button btn_detail;
        private TextView tv_style;
        private TextView tv_detail;
        private ImageView iv_status;

        View view;

        ChooseMoneyViewHolder(View view) {
            super(view);

            this.view = view;

            tv_price = (TextView) view.findViewById(R.id.tv_price);
            tv_num = (TextView) view.findViewById(R.id.tv_num);
            tv_goods = (TextView) view.findViewById(R.id.tv_goods);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_style = (TextView) view.findViewById(R.id.tv_style);
            tv_detail = (TextView) view.findViewById(R.id.tv_detail);
            iv_status = (ImageView) view.findViewById(R.id.iv_status);
//            btn_detail = (Button) view.findViewById(R.id.btn_detail);

//            view.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    if (itemClickListener != null) {
//                        itemClickListener.onclick();
//                    }
//
//                }
//            });

        }

    }

    public ItemClickListener itemClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onclick(int position);
    }
}
